// miniprogram/pages/my_info/my_info.js

import req from "../../utils/request";
import { toast } from "../../utils/toast";
import {login} from "../../utils/login.js";
import { REST_SCHOOL_ID_TO_NAME, USER_ADDRESS_LIST } from "../../messages/uris";

const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    campus: "",
    phone: "",
    deliver_address: "",
    index: 0,
    count: 0,
    schoolName: "", 
    //弹出层
    topTips: false,
    hide: false,
    tipValue: "",
  },
  /**
   * 加载用户信息
   */
  loadUserInfo: function () {
    const that = this;
    let user = wx.getStorageSync('userInfo')  
    this.setData({
      userInfo: user
    });
    // 获取学校name
    if (app.globalData.login == true && user) {
      req.get(REST_SCHOOL_ID_TO_NAME , {}, (res) => {
        that.setData({ schoolName: res.data.result.schoolName });
      },(res)=>{},(res)=>{},that);
    }

    // TODO 获取用户地址
    // req.get(USER_ADDRESS_LIST, { pageSize: 10, pageNo: 1 }, (res) => {
    //   let addresslist = res.data.result.records;
    //   if (!res.data.result.records || !addresslist) {
    //     return;
    //   } else {
    //     let userDefAddress = addresslist[0].dormAddress;
    //     addresslist.forEach((al) => {
    //       if (al.isDefault == 1) userDefAddress = al;
    //     });
    //     that.setData({ deliver_address: userDefAddress });
    //   }
    // });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: "请稍后",
      mask: true,
    });
    if (app.globalData.login == false) {
      login();
    }
    this.loadUserInfo();

    wx.hideLoading();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
